package com.travel.photos.ui.recommend;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.travel.R;
import com.travel.photos.bean.PhotoRecommentBean;
import com.travel.photos.image.ImageHepler;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by meidou on 2016/10/19.
 */
public class RecommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_TWO = 1; //recycleview
    private static final int TYPE_THREE = 2;//下拉加载更多;
    private Context context;//上下文
    private List<PhotoRecommentBean.DataBean.ListBean> totailist;

    public RecommentAdapter(Context context, List<PhotoRecommentBean.DataBean.ListBean> totailist) {
        this.context = context;
        this.totailist = totailist;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TWO) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.item_photo_recommend, parent, false));
            return holder;
        } else if (viewType == TYPE_THREE) {
            FootViewHolder footholder = new FootViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.photo_recycler_load_more, parent, false));
            return footholder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {//
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.mPhotoFoucsItemText.setText(totailist.get(position).getNickName());
            if(totailist.get(position).getQualification()!=null){
                myViewHolder.mPhotoFoucsItemText2.setText(totailist.get(position).getQualification());
            }else {
                myViewHolder.mPhotoFoucsItemText2.setVisibility(View.GONE);
            }
            ImageHepler.showImage(context, totailist.get(position).getPics().get(0), myViewHolder.mPhotoRecommendItemImageone, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(1), myViewHolder.mPhotoFoucsItemImage2, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(2), myViewHolder.mPhotoFoucsItemImage3, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(3), myViewHolder.mPhotoFoucsItemImage4, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(4), myViewHolder.mPhotoFoucsItemImagetwo, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(5), myViewHolder.mPhotoFoucsItemImagethree, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(6), myViewHolder.mPhotoFoucsItemImagefour, false);
            ImageHepler.showImage(context, totailist.get(position).getPics().get(7), myViewHolder.mPhotoRecommendItemImage1, false);
        } else if (holder instanceof FootViewHolder) {
            ((FootViewHolder) holder).tv_jiazai.setText("上拉加载更多...");
        }
    }

    @Override
    public int getItemCount() {
        return totailist.size() + 1;
    }


    //下拉加载更多布局
    class FootViewHolder extends RecyclerView.ViewHolder {
        TextView tv_jiazai;
        ProgressBar iv_jiazai;

        public FootViewHolder(View itemView) {
            super(itemView);
            tv_jiazai = (TextView) itemView.findViewById(R.id.tv_jiazai);
            iv_jiazai = (ProgressBar) itemView.findViewById(R.id.iv_jiazai);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_THREE;//
        } else {
            return TYPE_TWO;//
        }
    }

class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.photo_foucs_item_totall)
        TextView mPhotoFoucsItemText;
        @Bind(R.id.photo_foucs_item_xiangqing)
        TextView mPhotoFoucsItemText2;
        @Bind(R.id.photo_recommend_item_imageone1)
        ImageView mPhotoRecommendItemImageone;
        @Bind(R.id.photo_foucs_item_imagetwo2)
        ImageView mPhotoFoucsItemImagetwo;
        @Bind(R.id.photo_foucs_item_imagethree3)
        ImageView mPhotoFoucsItemImagethree;
        @Bind(R.id.photo_foucs_item_imagefour4)
        ImageView mPhotoFoucsItemImagefour;
        @Bind(R.id.photo_recommend_item_image1)
        ImageView mPhotoRecommendItemImage1;
        @Bind(R.id.photo_foucs_item_image2)
        ImageView mPhotoFoucsItemImage2;
        @Bind(R.id.photo_foucs_item_image3)
        ImageView mPhotoFoucsItemImage3;
        @Bind(R.id.photo_foucs_item_image4)
        ImageView mPhotoFoucsItemImage4;

    public MyViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
    }
    }
}
